<?php
/**
 * Created by PhpStorm.
 * User: tangbl
 * Date: 2019/1/25
 * Time: 9:55
 */
namespace app\wechat\common\model;
use think\Db;
use think\Exception;
use think\Model;

class Pub extends Model{

    public static $OK = 0;
    public static $IllegalAesKey = -41001;
    public static $IllegalIv = -41002;
    public static $IllegalBuffer = -41003;
    public static $DecodeBase64Error = -41004;

    //微信绑定手机
    public function bindModify($data, $user){

        if(!isset($data['mobile']))
            return json(['code'=>0,'msg'=>'手机号不能为空']);
        if(!isset($data['code']))
            return json(['code'=>0,'msg'=>'验证码不能为空']);
        //验证短信
        $class = new \app\common\util\message\Sms();
        $jsons = $class->checkCode($data['mobile'],$data['code']);
        if(empty($jsons['code'])){
            return $jsons;
        }

        try {
            Db::startTrans();
            $result = self::createInfo($user, $data);
            Db::commit();

            $users = \app\wechat\common\logic\Token::setUser($result);
            $token = \app\wechat\common\logic\Token::issue();
            return ['code'=>1,'token'=>$token,'data'=>$users];

        } catch (\Exception $e) {
            Db::rollback();
            if($e->getCode() == 1){
                $msg = $e->getMessage();
            }else{
                $msg = $e->getMessage();;
            }
            trace('bind.mobile:' . $e->getMessage().$e->getTraceAsString() . var_export($data, true), 'error');
            return ['code' => 0, 'msg' => $msg];
        }
    }

    //设置用户信息[强制授权]
    public static function createInfo($user, $data){

        $flag = Db::name('user')->where(['mobile' => $data['mobile']])->field('id,mobile,openid')->find();
        if(isset($flag['id'])){
            throw new Exception('该手机号已经绑定');
        }
        $flag = Db::name('user')->where(['openid' => $user['openid']])->field('id,mobile,openid')->find();
        if(isset($flag['id'])){
            throw new Exception('该微信用户已经绑定');
        }

        $referUser = [];
        if($data['frmid']){
            $referUser = Db::name('user')->strict(false)->where(['yqcode'=>$data['frmid']])->field('id,yqcode,frmid')->find();
        }
        $wechatUser = Db::name('user_wechat')->strict(false)->where(['openid' => $user['openid']])->find();
        $insert = [
            'userid' => createid(),
            'chnid' => isset($wechatUser['chnid']) ? $wechatUser['chnid'] : '',
            'frmid' => isset($referUser['yqcode']) ? $referUser['yqcode'] : '',
            'resid' => isset($referUser['frmid']) ? $referUser['frmid'] : '',
            'mobile' => $data['mobile'],
            'headwx' => $wechatUser['headwx'],
            'nickwx' => $wechatUser['nickwx'],
            'openid' => $wechatUser['openid'],
            'yqcode' => randomString(6),
            'ischeck' => 2,
            'isinfo' => 2,
            'acttime' => NOW_TIME,
            'actdate' => date('Ymd', NOW_TIME),
            'addtime' => NOW_TIME,
            'adddate' => date('Ymd', NOW_TIME),
        ];
        $flag = Db::name('user')->strict(false)->insertGetId($insert);
        if ($flag) {
            $userWechat = [
                'userid' => $insert['userid'],
                'frmid' => isset($referUser['yqcode']) ? $referUser['yqcode'] : '',
                'resid' => isset($referUser['frmid']) ? $referUser['frmid'] : '',
            ];
            $flag1 = Db::name('user_wechat')->strict(false)->where(['openid' => $user['openid']])->update($userWechat);
            if(!$flag1) {
                throw new Exception('更新用户信息失败');
            }
            return $insert;
        }else{
            throw new Exception('更新用户信息失败');
        }
    }

}